
package com.catsic.zcjc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;
import com.catsic.zcjc.condition.EquipFileDataCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.convert.ZcjcConverts;
import com.catsic.zcjc.dao.EquipFileDataDao;
import com.catsic.zcjc.entity.EquipFileData;
import com.catsic.zcjc.form.EquipFileDataForm;
import com.catsic.zcjc.service.EquipFileDataService;
import com.catsic.zcjc.specifiction.EquipFileDataSpec;


@Service("equipFileDataService")
public class EquipFileDataServiceImpl extends BaseServiceImpl<EquipFileData, String> implements EquipFileDataService {
	

	@Autowired
	private EquipFileDataDao equipFileDataDao;
	
	@Autowired
	private DrawerService drawerService;

	@Override
	public EquipFileData findByDataIdAndType(String dataId, int type) {
		List<EquipFileData> list = equipFileDataDao.findByDataIdAndType(dataId, type);
		
		if(CollectionUtils.isNotEmpty(list)){
			return list.get(0);
		}
		
		return null;
	}

	@Override
	public EquipFileData findById(String id) {
		EquipFileData file = getById(id);
		if(null == file){
			throw new CatsicException(CatsicExceptionCode.实体不存在, ZcjcConstants.EQUIP_FILE_DATA);
		}
		return file;
	}

	@Override
	public Page<EquipFileDataForm> findAll(EquipFileDataCondition condition, Pageable pageable) {
		EquipFileDataSpec spec = new EquipFileDataSpec(condition);
		Page<EquipFileData> page = equipFileDataDao.findAll(spec, pageable);
		List<EquipFileDataForm> listForm = new ArrayList<EquipFileDataForm>();
		for(EquipFileData equipFileData :page.getContent()){
			EquipFileDataForm form = ZcjcConverts.convert(equipFileData);
			
			
			listForm.add(form);
	    }
	    Page<EquipFileDataForm> page_ = new PageImpl<EquipFileDataForm>(listForm,pageable,page.getTotalElements());
	    return page_;
	}

	@Override
	public void findFormatDrawer(EquipFileDataForm form) {
		Drawer typeD = null;
		if(form.getType()!=null){
			typeD = drawerService.findByParentAndValue(ZcjcConstants.DRAWER_EQUIP_DATA_FILE_TYPE, form.getType().toString());
			if(typeD!=null){
				form.setCnType(typeD.getName());
			}
		}
	}
	

	


}
